#!/usr/bin/env python
# Lolcat - Translates strings into lolcat speak
# David Coles (239715), 2010

import random

# Add this to the start of all the strings
OHHAI = "Oh hai thare!"

# These are words that have a special mapping
SPECIALWORDS = {'long': 'loooooooooong',
        'hi': 'hai',
        'i': 'ai',
        'dude': 'd00d',
        'of': 'o',
        'me': 'meh',
        'my': 'mah',
        'we': 'iz',
        'to': '2',
        'the': 'teh',
        'you': 'u',
        'your': 'ur',
        'you\'re': 'ur',
        'very': 'vewee',
        'and': 'an',
        'people': 'ppl',
        'on': 'onz',
        'made': 'maded',
        'ate': 'eated',
        }

# We randomly replace the end of a sentance with one of these.
ENDINGS = [". kthx?", ". Srsly.", ", an stuffs.", "s.", " lol whut?"]

def lolrofl(string):
    # Do a simple tokenisation - just break on whitespace
    tokens = string.split()
    loltokens = []
    for token in tokens:
        # First see if we know this word
        # Don't worry about capitalization - it looks better without it!
        if token.lower() in SPECIALWORDS:
            token = SPECIALWORDS[token.lower()]
        else:
            # Look at the endings of words
            if token.endswith("er"):
                token = token[:-2] + "r"
            if token.endswith("ers"):
                token = token[:-3] + "rs"
            if token.endswith("es"):
                token = token[:-2] + "z"
            if token.endswith("s"):
                token = token[:-1] + "z"
            if token.endswith("rough"):
                token = token[:-5] + "oo"

            # Make some Homonymous "misstakez"
            # FIXME: It would be best to do this based on sounds rather than 
            # letter pairs - perhaps matching soundex? For the time being just 
            # try some obvious guesses
            token = token.replace('oo', 'ooo')
            token = token.replace('ee', 'ea')
            token = token.replace('oa', 'ow')
            token = token.replace('ay', 'ae')
            token = token.replace('ove', 'uv')
            token = token.replace('ate', 'aet')
            token = token.replace('ike', 'iek')

            # Punctuation? Lol, whut?
            token = token.replace("!", "!!!1!")
            token = token.replace(';', '~')
            token = token.replace(':', '~~~')

        # Finally add in the optional endings
        if token.endswith("."):
            if random.random() <= 0.5:
                token = token[:-1] + random.choice(ENDINGS)

        loltokens.append(token)

    # Return the tokens plus prefix
    return OHHAI + " " + " ".join(loltokens)

if __name__ == "__main__":
    # Test case - Bram Stoker's Dracula
    dracula = (
"""All day long we seemed to dawdle through a country which was full of
beauty of every kind.  Sometimes we saw little towns or castles on the
top of steep hills such as we see in old missals; sometimes we ran by
rivers and streams which seemed from the wide stony margin on each
side of them to be subject to great floods.  It takes a lot of water,
and running strong, to sweep the outside edge of a river clear."""
)
    print lolrofl(dracula)


